using System.Reflection;

namespace Game.Tools
{
    public static class ReflectionDebugTestTool
    {
        public static object GetFieldValue(this object t, string name)
        {
            FieldInfo info = t.GetType().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic);
            if (info != null)
            {
                return info.GetValue(t);
            }
            return null;
        }

        public static object GetStaticFieldValue(this object t, string name)
        {
            FieldInfo info = t.GetType().GetField(name, BindingFlags.Static | BindingFlags.NonPublic);
            if (info != null)
            {
                return info.GetValue(t);
            }
            return null;
        }

        public static FieldInfo SetFieldValue(this object t, string name, object val)
        {
            FieldInfo info = t.GetType().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic);
            if (info != null)
            {
                info.SetValue(t, val);
            }
            return info;
        }

        public static FieldInfo SetStaticFieldValue(this object t, string name, object val)
        {
            FieldInfo info = t.GetType().GetField(name, BindingFlags.Static | BindingFlags.NonPublic);
            if (info != null)
            {
                info.SetValue(t, val);
            }
            return info;
        }

        public static PropertyInfo SetPropertyValue(this object t, string name, object val)
        {
            PropertyInfo info = t.GetType().GetProperty(name);
            if (info != null)
            {
                info.SetValue(t, val, null);
            }
            return info;
        }

        public static MethodInfo ExecuteMethod(this object t, string name, object[] parameters)
        {
            MethodInfo info = t.GetType().GetMethod(name, BindingFlags.Instance | BindingFlags.NonPublic);
            if (info != null)
            {
                info.Invoke(t, parameters);
            }
            return info;
        }
    }
}